package com.deliveryservicesystem.presentation.desktop.addpanel;

import com.deliveryservicesystem.app.exceptions.OutputError;
import com.deliveryservicesystem.app.exceptions.PreexistingEntityException;
import com.deliveryservicesystem.app.factory.AppFactory;
import com.deliveryservicesystem.app.util.AppUtils;
import com.deliveryservicesystem.app.validation.SubscripionValidator;
import com.deliveryservicesystem.app.validation.ValidationUtil;
import com.deliveryservicesystem.dataaccess.PersistenceDbManager;
import com.deliveryservicesystem.model.Billing;
import com.deliveryservicesystem.model.Customer;
import com.deliveryservicesystem.model.Delivery;
import com.deliveryservicesystem.model.Subscription;
import com.deliveryservicesystem.presentation.desktop.InfoLableChangeHandler;
import com.deliveryservicesystem.presentation.desktop.MatchDialog;
import com.deliveryservicesystem.service.crud.impl.SubscriptionCrudServiceImpl;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.view.ValidationComponentUtils;
import com.jgoodies.validation.view.ValidationResultViewFactory;
import com.toedter.calendar.JDateChooser;
import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 * File Name: AddEditSubscripionPanel.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.presentation.desktop.addpanel
 * Author: Lance
 * Date: Jun 27, 2012
 * Editor:
 */
public class AddEditSubscritpionDialog extends javax.swing.JDialog
{

    private static final long serialVersionUID = 1L;
    private SubscriptionCrudServiceImpl subscriptionCrudService;
    private Subscription subscripionFromDb;
    private JDateChooser dateChooser;
    private PersistenceDbManager dbManager;

    public AddEditSubscritpionDialog(java.awt.Frame parent, boolean modal, HashMap<String, String> values)
    {
        super(parent, modal);
        dbManager = PersistenceDbManager.getInstance();
        dateChooser = new JDateChooser(null, new Date(), null, new com.toedter.calendar.JSpinnerDateEditor());
        dateChooser.setDateFormatString("dd MMM yyyy");
        dateChooser.getJCalendar().setTodayButtonVisible(true);
        initComponents();
        cbBillingNo.setSelectedIndex(-1);
        cbCustNo.setSelectedIndex(-1);
        cbDeliverNo.setSelectedIndex(-1);
        subscriptionCrudService = new SubscriptionCrudServiceImpl();
        if (!values.isEmpty())
        {
            cbDeliverNo.setSelectedItem(new Delivery(Integer.parseInt(values.get("DELIVERY NUMBER"))));
            cbBillingNo.setSelectedItem(new Billing(Integer.parseInt(values.get("BILLING NUMBER"))));
            try
            {
                String dateFormatConverter = AppUtils.dateFormatConverter(values.get("SUBSCRIPTION DATE"), "dd MMM yyyy", "yyyy-MM-dd", Locale.ENGLISH);
                dateChooser.setDate(java.sql.Date.valueOf(dateFormatConverter));
            }
            catch (ParseException ex)
            {
                OutputError.toLog(ex);
                OutputError.toUser(ex);
            }

            lblSubNo.setText(values.get("SUBSCRIPTION NUMBER"));
            txtTotalAmount.setText(values.get("TOTAL AMOUNT"));
            cbCustNo.setSelectedItem(new Customer(Integer.parseInt(values.get("CUSTOMER NUMBER"))));
            txtBaseAmount.setText(values.get("BASE AMOUNT"));
            subscripionFromDb = subscriptionCrudService.findById(Integer.parseInt(values.get("SUBSCRIPTION NUMBER")));
        }

        ValidationComponentUtils.setInputHint(dateChooser, "Please enter the delivery date");
        ValidationComponentUtils.setInputHint(txtBaseAmount, "Please enter a base amount");
        ValidationComponentUtils.setInputHint(txtTotalAmount, "Please enter a total amount");
        ValidationComponentUtils.setInputHint(cbBillingNo, "Please select a billing number");
        ValidationComponentUtils.setInputHint(cbCustNo, "Please select a customer number");
        ValidationComponentUtils.setInputHint(cbDeliverNo, "Please select a delivery number");

        KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(new InfoLableChangeHandler(lblInfo));
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = dbManager.getEntityManager();
        billingQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT b FROM Billing b");
        deliveryQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT d FROM Delivery d");
        customerQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Customer c");
        billingList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : billingQuery.getResultList();
        customerList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : customerQuery.getResultList();
        deliveryList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : deliveryQuery.getResultList();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        lblSubNo = new javax.swing.JLabel();
        lblInfo = new javax.swing.JLabel();
        bntMatchBilling = new javax.swing.JButton();
        lblValidationErrors = new javax.swing.JLabel();
        lblValidationErrors.setVisible(false);
        txtDate = new javax.swing.JTextField();
        btnCancel = new javax.swing.JButton();
        btnOk = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        txtBaseAmount = new javax.swing.JTextField();
        btnMatchCustomer = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        txtTotalAmount = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        cbDeliverNo = new javax.swing.JComboBox();
        cbBillingNo = new javax.swing.JComboBox();
        btnMatchDelivery = new javax.swing.JButton();
        cbCustNo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Subscription Number");

        lblSubNo.setText("New Number is Generated");

        lblInfo.setIcon(ValidationResultViewFactory.getInfoIcon());
        lblInfo.setText("Info Lable");

        bntMatchBilling.setText("Match");
        bntMatchBilling.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntMatchBillingActionPerformed(evt);
            }
        });

        lblValidationErrors.setIcon(ValidationResultViewFactory.getCheckIcon());
        lblValidationErrors.setText("Validation Errors");

        txtDate.setText("Date");

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        jLabel5.setText("Subscription Date");

        jLabel9.setText("Total Amount");

        jLabel8.setText("Base Amount");

        btnMatchCustomer.setText("Match");
        btnMatchCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMatchCustomerActionPerformed(evt);
            }
        });

        jLabel10.setText("Delivery Number");

        jLabel12.setText("Billing Number");

        jLabel11.setText("Customer Number");

        cbDeliverNo.setRenderer(new DefaultListCellRenderer()
            {

                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                {
                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    if (value instanceof Delivery)
                    {
                        Delivery delivery = (Delivery) value;
                        setText(delivery.getDeliveryNo().toString());
                    }
                    return this;
                }
            });

            org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, deliveryList, cbDeliverNo, "delivery");
            bindingGroup.addBinding(jComboBoxBinding);
            org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, deliveryList, org.jdesktop.beansbinding.ObjectProperty.create(), cbDeliverNo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "deliveryNo");
            bindingGroup.addBinding(binding);

            cbBillingNo.setRenderer(new DefaultListCellRenderer()
                {

                    @Override
                    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                    {
                        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                        if (value instanceof Billing)
                        {
                            Billing billing = (Billing) value;
                            setText(billing.getBillingNo().toString());
                        }
                        return this;
                    }
                });

                jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, billingList, cbBillingNo, "billingno");
                bindingGroup.addBinding(jComboBoxBinding);
                binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, billingList, org.jdesktop.beansbinding.ObjectProperty.create(), cbBillingNo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "billing");
                bindingGroup.addBinding(binding);

                btnMatchDelivery.setText("Match");
                btnMatchDelivery.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        btnMatchDeliveryActionPerformed(evt);
                    }
                });

                cbCustNo.setRenderer(new DefaultListCellRenderer()
                    {

                        @Override
                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                        {
                            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                            if (value instanceof Customer)
                            {
                                Customer customer = (Customer) value;
                                setText(customer.getCustomerNo().toString());
                            }
                            return this;
                        }
                    });

                    jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, customerList, cbCustNo, "cuso");
                    bindingGroup.addBinding(jComboBoxBinding);
                    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, customerList, org.jdesktop.beansbinding.ObjectProperty.create(), cbCustNo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "cusono");
                    bindingGroup.addBinding(binding);

                    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                    getContentPane().setLayout(layout);
                    layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel11)
                                        .addComponent(jLabel10))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(cbDeliverNo, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(btnMatchDelivery))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(cbCustNo, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(btnMatchCustomer)))
                                    .addGap(0, 198, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(jLabel1)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(lblSubNo))
                                                .addGroup(layout.createSequentialGroup()
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel12)
                                                        .addComponent(jLabel9)
                                                        .addComponent(jLabel8)
                                                        .addComponent(jLabel5))
                                                    .addGap(20, 20, 20)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(txtBaseAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(txtTotalAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(layout.createSequentialGroup()
                                                            .addComponent(cbBillingNo, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addComponent(bntMatchBilling))
                                                        .addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addComponent(lblValidationErrors))
                                            .addGap(0, 188, Short.MAX_VALUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addGap(0, 0, Short.MAX_VALUE)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(lblInfo, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                    .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(btnCancel)))))
                                    .addContainerGap())))
                    );
                    layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(lblSubNo))
                            .addGap(11, 11, 11)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5)
                                .addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel8)
                                .addComponent(txtBaseAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9)
                                .addComponent(txtTotalAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel12)
                                .addComponent(cbBillingNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(bntMatchBilling))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel11)
                                .addComponent(cbCustNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnMatchCustomer))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel10)
                                .addComponent(cbDeliverNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnMatchDelivery))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(lblValidationErrors)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 144, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnCancel)
                                .addComponent(btnOk))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lblInfo)
                            .addContainerGap())
                    );

                    bindingGroup.bind();

                    pack();
                }// </editor-fold>//GEN-END:initComponents

    private void bntMatchBillingActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_bntMatchBillingActionPerformed
    {//GEN-HEADEREND:event_bntMatchBillingActionPerformed
        try
        {
            MatchDialog frame = new MatchDialog(null, true, "BEGIN ? := GET_DATA.BILLINGS; END;");
            AppUtils.locateOnScreenCenter(frame);
            frame.setVisible(true);
            cbBillingNo.setSelectedItem(new Billing(frame.getReturnStatus()));
        }
        catch (SQLException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_bntMatchBillingActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCancelActionPerformed
    {//GEN-HEADEREND:event_btnCancelActionPerformed
        if (AppUtils.askExitConfirm() == JOptionPane.YES_OPTION)
        {
            this.dispose();
        }
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnOkActionPerformed
    {//GEN-HEADEREND:event_btnOkActionPerformed
        Date subscriptionDate = dateChooser.getDate();
        String baseAmount = txtBaseAmount.getText();
        String totalAmount = txtTotalAmount.getText();
        Billing billing = (Billing) cbBillingNo.getSelectedItem();
        Customer customer = (Customer) cbCustNo.getSelectedItem();
        Delivery delivery = (Delivery) cbDeliverNo.getSelectedItem();
        String subscriptionNo = lblSubNo.getText();

        SubscripionValidator validator = new SubscripionValidator(dateChooser, baseAmount, totalAmount, billing, customer, delivery);
        ValidationResult validationResult = validator.validate(new ValidationResult());
        ValidationUtil.displayErrorsToLable(validationResult, lblValidationErrors);

        if (validationResult.isEmpty())
        {
            Subscription subscription = AppFactory.createSubscription(subscriptionNo, subscriptionDate, baseAmount, totalAmount, billing, customer, delivery);

            int askUserQuestion = AppUtils.askUserQuestion(this, "Add/Edit the following?\n" + subscription, "Add/Edit", JOptionPane.YES_NO_CANCEL_OPTION);

            if (askUserQuestion == JOptionPane.YES_OPTION)
            {
                try
                {
                    if (subscription.getSubscriptionNo() == null)
                    {
                        subscriptionCrudService.create(subscription);
                        AppUtils.showUserMessage(this, "Added the following:\n" + subscription, "Add/Edit", JOptionPane.INFORMATION_MESSAGE);
                    }
                    else
                    {
                        AppFactory.createUpdatedSubscription(subscription, subscripionFromDb);
                        subscriptionCrudService.edit(subscripionFromDb);
                        AppUtils.showUserMessage(this, "Updated the following:\n" + subscription, "Add/Edit", JOptionPane.INFORMATION_MESSAGE);
                    }
                    this.dispose();
                }
                catch (PreexistingEntityException ex)
                {
                    OutputError.toLog(ex);
                    OutputError.toUser(ex);
                }
                catch (Exception ex)
                {
                    OutputError.toLog(ex);
                    OutputError.toUser(ex);
                }
            }
        }
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnMatchCustomerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnMatchCustomerActionPerformed
    {//GEN-HEADEREND:event_btnMatchCustomerActionPerformed
        try
        {
            MatchDialog frame = new MatchDialog(null, true, "BEGIN ? := GET_DATA.CUSTOMERS; END;");
            AppUtils.locateOnScreenCenter(frame);
            frame.setVisible(true);
            cbCustNo.setSelectedItem(new Customer(frame.getReturnStatus()));
        }
        catch (SQLException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_btnMatchCustomerActionPerformed

    private void btnMatchDeliveryActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnMatchDeliveryActionPerformed
    {//GEN-HEADEREND:event_btnMatchDeliveryActionPerformed
        try
        {
            MatchDialog frame = new MatchDialog(null, true, "BEGIN ? := GET_DATA.DELIVERIES; END;");
            AppUtils.locateOnScreenCenter(frame);
            frame.setVisible(true);
            cbDeliverNo.setSelectedItem(new Delivery(frame.getReturnStatus()));
        }
        catch (SQLException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_btnMatchDeliveryActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<com.deliveryservicesystem.model.Billing> billingList;
    private javax.persistence.Query billingQuery;
    private javax.swing.JButton bntMatchBilling;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnMatchCustomer;
    private javax.swing.JButton btnMatchDelivery;
    private javax.swing.JButton btnOk;
    private javax.swing.JComboBox cbBillingNo;
    private javax.swing.JComboBox cbCustNo;
    private javax.swing.JComboBox cbDeliverNo;
    private java.util.List<com.deliveryservicesystem.model.Customer> customerList;
    private javax.persistence.Query customerQuery;
    private java.util.List<com.deliveryservicesystem.model.Delivery> deliveryList;
    private javax.persistence.Query deliveryQuery;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblSubNo;
    private javax.swing.JLabel lblValidationErrors;
    private javax.swing.JTextField txtBaseAmount;
    private javax.swing.JTextField txtDate;
    private javax.swing.JTextField txtTotalAmount;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
